﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;

namespace SharePointDatabaseExplorer
{
    public partial class frmVersions : Form
    {
        private int _ID;
        private string _DocID;
        private string _ListID;
        private string _ListName;
        private string _SiteID;
        private Hashtable _MetadataFields;
        
        public frmVersions()
        {
            InitializeComponent();
        }

        public int ID
        {
            get
            {
                return _ID;
            }
            set
            {
                _ID = value;
            }
        }

        public string DocID
        {
            get
            {
                return _DocID;
            }
            set
            {
                _DocID = value;
            }
        }

        public string ListID
        {
            get
            {
                return _ListID;
            }
            set
            {
                _ListID = value;
            }
        }
        public string SiteID
        {
            get
            {
                return _SiteID;
            }
            set
            {
                _SiteID = value;
            }
        }
        public string ListName
        {
            get
            {
                return _ListName;
            }
            set
            {
                _ListName = value;
            }
        }

        public Hashtable MetadataFields
        {
            get
            {
                return _MetadataFields;
            }
            set
            {
                _MetadataFields = value;
            }
        }
        private void frmVersions_Load(object sender, EventArgs e)
        {
            SetScreenResolution();
            lblListName.Text = "List Name = " + this.ListName + " Item ID: " + this.ID;

            StringBuilder sbColumns = new StringBuilder();

            foreach (DictionaryEntry entry in this.MetadataFields)
            {
                string sDisplayName = entry.Key.ToString();
                string sColumnName = entry.Value.ToString();
                sbColumns.Append(sColumnName + " as '" + sDisplayName + "',");
            }

            sbColumns.Remove(sbColumns.Length - 1, 1);

            //string sSQL = "SELECT tp_Id, " + sbColumns.ToString() + " FROM AllUserData ad WHERE ad.tp_ListId = '" +  this.ListID + "' and ad.tp_Version = (select max(tp_Version) from AllUserData ad1 where tp_ListID = '" + sGUID + "' and ad.tp_ID = ad1.tp_ID) " + sbWhere;
            string sSQL = "SELECT tp_Id, tp_Version as 'Document Version', " + sbColumns.ToString() + " FROM AllUserData WHERE tp_ID = " + this.ID + " AND tp_ListId = '" + this.ListID + "' ORDER BY tp_Version";
            DataSet dsResults = Database.RunQuery(sSQL);
            
            dgResults.AutoGenerateColumns = true;
            dgResults.DataSource = dsResults.Tables[0];
        }

        private void btnExportDocument_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (dgResults.SelectedRows.Count > 0)
            {
                Boolean bSuccess = false;

                SharePoint objSharePoint = new SharePoint();

                foreach (DataGridViewRow row in dgResults.SelectedRows)
                {
                    string sID = row.Cells[0].Value.ToString();
                    objSharePoint.ID = Convert.ToInt16(sID);
                    objSharePoint.ListID = this.ListID;
                    objSharePoint.SiteID = this.SiteID;
                    objSharePoint.VersionID = Convert.ToInt16(row.Cells[1].Value.ToString());
                    bSuccess = objSharePoint.ExportFile(Convert.ToInt16(row.Cells[1].Value.ToString()));
                }

                if (bSuccess)
                {
                    this.Cursor = Cursors.Default;
                    MessageBox.Show("Files successfully exported to " + Database._FileOutputPath, "File Exported", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    this.Cursor = Cursors.Default;
                    MessageBox.Show("There are no files to export", "No Files Exported", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                this.Cursor = Cursors.Default;
                MessageBox.Show("Select a file(s) to export", "Select File(s)", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btnExportMetadata_Click(object sender, EventArgs e)
        {
            if (dgResults.Rows.Count > 0)
            {
                string sExportFile = Database._FileOutputPath + @"\" + ListName + ".csv";
                SharePoint.ExportMetadataToCSV(dgResults, sExportFile);

                MessageBox.Show("Successfully exported the results to: " + sExportFile, "Results Exported", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Select a list and retrieve the results to export", "No Results to Export", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void SetScreenResolution()
        {
            dgResults.Width = this.Width - 100;
            dgResults.Height = this.Height - 100;
        }

        private void frmVersions_Resize(object sender, EventArgs e)
        {
            SetScreenResolution();
        }

    }
}
